package com.ruoyi.project.applet.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.applet.domain.Good;
import com.ruoyi.project.applet.service.IGoodService;
import com.ruoyi.project.system.domain.SysUserWx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 平台商品Controller
 * 
 * @author ruoyi
 * @date 2025-02-07
 */
@RestController
@RequestMapping("/system/good")
public class GoodController extends BaseController
{
    @Autowired
    private IGoodService goodService;

    /**
     * 查询平台商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Good good)
    {
        startPage();
        List<Good> list = goodService.selectGoodList(good);
        return getDataTable(list);
    }

    /**
     * 我的发布
     */
    @GetMapping("/list/wx")
    public TableDataInfo listwx(Good good)
    {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx, "请先登录");
        good.setUserId(userWx.getOpenId());
        startPage();
        List<Good> list = goodService.selectGoodList(good);
        return getDataTable(list);
    }



    /**
     * 获取平台商品详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx, "请先登录");
        return AjaxResult.success(goodService.selectGoodById(id, userWx.getOpenId()));
    }


    /**
     * 发布信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody Good good)
    {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx,"请先登录");
        if(Constants.IS_VIP.equals(userWx.getIsVip())){
            good.setUserId(userWx.getOpenId());
            return AjaxResult.success(goodService.insertGood(good));
        } else {
            return AjaxResult.error("请先开通会员");
        }
    }


    /**
     * 编辑商品信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Good good)
    {
        return toAjax(goodService.updateGood(good));
    }

    /**
     * 删除平台商品
     */
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(goodService.deleteGoodById(id));
    }
}
